
// A1_compute_price_diffs
//==============================================================================

// Description: This file computes average differences between import and export prices for each country that are
// then used in Figure 1

clear
clear matrix
set more off
set matsize 5000

cd "D:\data_replication"

// Import and format trade and income data
//------------------------------------------------------------------------------

use estimation\1_data_format\data_base.dta, clear

keep if year==2007

gen price_weighted = price_pc8plus * exp_share
sort year quarter product_id declarant
by year quarter product_id declarant: gen price_dom_temp = price_pc8plus if declarant == partner
by year quarter product_id declarant: egen price_imp_temp = mean(price_pc8plus) if declarant != partner
by year quarter product_id declarant: egen price_sum_temp = sum(price_weighted) if declarant != partner
by year quarter product_id declarant: egen price_sum_temp_exEU = sum(price_weighted) if declarant != partner & partner > 100
by year quarter product_id declarant: egen price_sum_temp_asia = sum(price_weighted) if declarant != partner & partner > 600 & partner < 800 
by year quarter product_id declarant: egen share_imp_temp = sum(exp_share) if declarant != partner
by year quarter product_id declarant: egen share_imp_temp_exEU = sum(exp_share) if declarant != partner & partner > 100
by year quarter product_id declarant: egen share_imp_temp_asia = sum(exp_share) if declarant != partner & partner > 600 & partner < 800 
by year quarter product_id declarant: egen price_dom = mean(price_dom_temp)
by year quarter product_id declarant: egen price_imp = mean(price_imp_temp)
by year quarter product_id declarant: egen share_imp = mean(share_imp_temp)
by year quarter product_id declarant: egen price_sum = mean(price_sum_temp)
by year quarter product_id declarant: egen share_imp_exEU = mean(share_imp_temp_exEU)
by year quarter product_id declarant: egen price_sum_exEU = mean(price_sum_temp_exEU)
by year quarter product_id declarant: egen share_imp_asia = mean(share_imp_temp_asia)
by year quarter product_id declarant: egen price_sum_asia = mean(price_sum_temp_asia)
by year quarter product_id declarant: egen price_imp_exEU_temp = mean(price_pc8plus) if declarant != partner & partner > 100
by year quarter product_id declarant: egen price_imp_asia_temp = mean(price_pc8plus) if declarant != partner & partner > 600 & partner < 800 
by year quarter product_id declarant: egen price_imp_exEU = mean(price_imp_exEU_temp)
by year quarter product_id declarant: egen price_imp_asia = mean(price_imp_asia_temp)
gen price_imp_weighted = price_sum / share_imp
gen price_imp_weighted_exEU = price_sum_exEU / share_imp_exEU
gen price_imp_weighted_asia = price_sum_asia / share_imp_asia

by year quarter product_id declarant: keep if _n == 1

drop price_dom_temp price_imp_temp


// Construct median price differences
//------------------------------------------------------------------------------

gen diff_price = log(price_dom) - log(price_imp)
gen diff_price_exEU = log(price_dom) - log(price_imp_exEU)  
gen diff_price_asia = log(price_dom) - log(price_imp_asia)  
gen diff_price_weighted = log(price_dom) - log(price_imp_weighted) 
gen diff_price_weighted_exEU = log(price_dom) - log(price_imp_weighted_exEU) 
gen diff_price_weighted_asia = log(price_dom) - log(price_imp_weighted_asia) 

sort declarant
by declarant: egen diff_med = median(diff_price)
by declarant: egen diff_med_exEU = median(diff_price_exEU)
by declarant: egen diff_med_asia = median(diff_price_asia)
by declarant: egen diff_med_weighted = median(diff_price_weighted)
by declarant: egen diff_med_weighted_exEU = median(diff_price_weighted_exEU)
by declarant: egen diff_med_weighted_asia = median(diff_price_weighted_asia)


// Run regressions and save output
//------------------------------------------------------------------------------

reg diff_med i.declarant, vce(robust)
estout using statistics_and_graphs\import_vs_export_prices\output_files\results_diff_med.txt, replace
reg diff_med_exEU i.declarant, vce(robust)
estout using statistics_and_graphs\import_vs_export_prices\output_files\results_diff_med_exEU.txt, replace
reg diff_med_asia i.declarant, vce(robust)
estout using statistics_and_graphs\import_vs_export_prices\output_files\results_diff_med_asia.txt, replace
reg diff_med_weighted i.declarant, vce(robust)
estout using statistics_and_graphs\import_vs_export_prices\output_files\results_diff_med_weighted.txt, replace
reg diff_med_weighted_exEU i.declarant, vce(robust)
estout using statistics_and_graphs\import_vs_export_prices\output_files\results_diff_med_weighted_exEU.txt, replace
reg diff_med_weighted_asia i.declarant, vce(robust)
estout using statistics_and_graphs\import_vs_export_prices\output_files\results_diff_med_weighted_asia.txt, replace


